package org.finalministry.message;

import java.io.Serializable;

public class Message implements Serializable {

   /**
    * Comment for <code>serialVersionUID</code>
    */
   private static final long serialVersionUID = -2403055978626483841L;

   protected String messageText = "";

   /**
    * The message Arguments
    */
   protected Object[] arguments = new Object[0];

   /**
    * The Message text without argument replaces
    * 
    * @return String The Message text without argument replaces
    */
   public String getMessageText() {

      return messageText;
   }

   /**
    * Set the Message text includes argument placeholder {0}..
    * 
    * @param messageText Set the Message text includes argument placeholder {0}
    */
   public void setMessageText(String messageText) {

      this.messageText = messageText;
   }

   /**
    * Returns the message arguments Object Array
    * 
    * @return The message arguments Object Array
    */
   public Object[] getArguments() {

      return arguments;
   }

   /**
    * Set the message arguments
    * 
    * @param arguments Message arguments as Object Array
    */
   public void setArguments(Object[] arguments) {

      this.arguments = arguments;
   }

   /**
    * Constructor
    * 
    * @param messageText the message text with argument placeholders {0} {1} ..
    * @param arguments Message arguments as object array
    */
   public Message(String messageText, Object[] arguments) {

      super();
      this.messageText = messageText;
      this.arguments = arguments;
   }

   /**
    * Replace the message placeholder with the argument
    * 
    * @return Message string with replaced arguments
    */
   public String getFormattedMessageText() {

      return MessageFormatter.replaceArguments(messageText, arguments);
   }

   /**
    * Constructor
    * 
    * @param messageText the message text without arguments
    */
   public Message(String messageText) {

      super();
      this.messageText = messageText;
      this.arguments = new Object[0];
   }

   /**
    * Return the message with replaced placeholder
    */
   public String toString() {

      if (messageText != null) {
         return this.getFormattedMessageText();
      } else {
         messageText = "";
         return this.getFormattedMessageText();
      }
   }
}
